<?php
/**
 * @File	  : users_model.php
 * @Project	  : vncphathoc
 * @author	  : ThanhMinh
 * @email	  : thanhminhld@yahoo.com - thanhminhld@gmail.com
 * @copyright : 2009
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users_model extends Base_Model
{
	public function __construct()
	{
		parent::__construct();
		$this->_tbl       = 'users';
		$this->_primaryId = 'user_id';
	}
	/**
	 * checkLogin : check user name and pass when login
	 * @access public
	 * @param
	 * @return array
	 */
	public function loadUserinfo ()
	{
	    $arrWhere = array('user_active' => 1,
	                      'user_rule'   => 1);
	    $query = $this->db->get_where($this->_tbl, $arrWhere, 1, 0);
        $arrList = array();
        if ($query->num_rows() > 0) {
            foreach($query->result() as $row) {
                $arrList = $row;
                break;
            }
        }
		return $arrList;
	}
	/**
	 * checkLogin : check user name and pass when login
	 * @access public
	 * @param $userName
	 * @param $passWord
	 * @return boolean
	 */
	public function checkLogin($userName, $passWord)
	{
        $result = false;
        if (empty($userName) || empty($passWord) || is_array($userName) || is_array($passWord)) {
			return $result;
        }

        $password = dohash($passWord, 'md5');
        $query = $this->db->get_where( $this->_tbl, array('user_access' => $userName,
														  'user_password' => $password));
        if ($query->num_rows() > 0) {
            $result = $query->row_object();
        }
        return $result;
	}

    public function hasLogin()
    {
        if (!$this->session->userdata('adm_login_id')) {
            header('Location:index.php?c=login');
        } else {
        	$users = $this->get_row( array('user_rule' => $this->session->userdata('adm_login_id')) );
        	$this->msmarty->assign('adm_username', $users->user_name);
        }
    }
    public function logout()
    {
        $lang_tag = $this->session->userdata('lang_tag');
        $this->session->sess_destroy();
        $this->session->set_userdata($lang_tag);
        header('Location:index.php?c=login');
    }
    /**
     * storeDB
     *
     */
    public function storeDB($data = array(), $primaryId)
    {
        if (!$this->db->field_exists($primaryId, $this->_tbl)) {
            return FALSE;
        }
        if (!array_key_exists($primaryId, $data)) {
            return FALSE;
        }
        $this->db->where($this->_primaryId, $data[$primaryId]);
        $ret = $this->db->update($this->_tbl, $data);

        return $ret;
    }
}
?>